package huanghui.netstorage.dao;

import huanghui.netstorage.modle.EntryFile;
import huanghui.netstorage.util.NetStorage;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * EntryFile entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see huanghui.netstorage.modle.EntryFile
 * @author MyEclipse Persistence Tools
 */

public class EntryfileDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(EntryfileDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String UPLOAD_TIME = "uploadTime";
	public static final String SIZE = "size";
	public static final String STATE = "state";
	public static final String PATH = "path";
	public static final String MD5 = "md5";

	public int save(EntryFile transientInstance) {
		int result = NetStorage.ACK_FAIL;
		log.debug("saving EntryFile instance");
		try {
			Integer id	= (Integer)getSession().save(transientInstance);
			log.debug("save successful");
			result = id;
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
		return result;
	}

	public void delete(EntryFile persistentInstance) {
		log.debug("deleting EntryFile instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public EntryFile findById(java.lang.Integer id) {
		log.debug("getting EntryFile instance with id: " + id);
		try {
			EntryFile instance = (EntryFile) getSession().get(
					"huanghui.netstorage.modle.EntryFile", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(EntryFile instance) {
		log.debug("finding EntryFile instance by example");
		try {
			List results = getSession().createCriteria(
					"huanghui.netstorage.modle.EntryFile").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding EntryFile instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from EntryFile as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByUploadTime(Object uploadTime) {
		return findByProperty(UPLOAD_TIME, uploadTime);
	}

	public List findBySize(Object size) {
		return findByProperty(SIZE, size);
	}

	public List findByState(Object state) {
		return findByProperty(STATE, state);
	}

	public List findByPath(Object path) {
		return findByProperty(PATH, path);
	}

	public List findByMd5(Object md5) {
		return findByProperty(MD5, md5);
	}

	public List findAll() {
		log.debug("finding all EntryFile instances");
		try {
			String queryString = "from EntryFile";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public EntryFile merge(EntryFile detachedInstance) {
		log.debug("merging EntryFile instance");
		try {
			Session session = getSession();
			session.beginTransaction();
			EntryFile result = (EntryFile) session.merge(detachedInstance);
			session.getTransaction().commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
	
	public int modifyEntryFileState(int id, int state)
	{
		String sql = "update entryfile set state = "+state+"  where id="+id;
		Session session = getSession();
		Transaction trans = session.beginTransaction();
		trans.begin();
		SQLQuery sQLQuery=session.createSQLQuery(sql);
		return sQLQuery.executeUpdate(); 
	}

	public void attachDirty(EntryFile instance) {
		log.debug("attaching dirty EntryFile instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(EntryFile instance) {
		log.debug("attaching clean EntryFile instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}